package com.zeta.eduservice.service.impl;

import com.zeta.eduservice.entity.question.EduChapter;
import com.zeta.eduservice.entity.question.EduChapterAndQuestionTypeSys;
import com.zeta.eduservice.entity.question.EduPaper;
import com.zeta.eduservice.entity.question.EduPaperAndQuestionTypeSys;
import com.zeta.eduservice.entity.question.EduPaperContent;
import com.zeta.eduservice.entity.question.EduPaperContentAnswer;
import com.zeta.eduservice.entity.question.EduQuestionTypeSys;
import com.zeta.eduservice.entity.vo.ChapterAndQuestionTypeSysVo;
import com.zeta.eduservice.entity.vo.ChapterVo;
import com.zeta.eduservice.entity.vo.CourseInfoForm;
import com.zeta.eduservice.entity.vo.PaperAndQuestionTypeSysVo;
import com.zeta.eduservice.entity.vo.PaperAnswerVo;
import com.zeta.eduservice.entity.vo.PaperListVo;
import com.zeta.eduservice.entity.vo.PaperVo;
import com.zeta.eduservice.mapper.EduPaperMapper;
import com.zeta.eduservice.service.EduPaperAndQuestionTypeSysService;
import com.zeta.eduservice.service.EduPaperContentAnswerService;
import com.zeta.eduservice.service.EduPaperContentService;
import com.zeta.eduservice.service.EduPaperService;
import com.zeta.eduservice.service.EduQuestionTypeSysService;
import com.zeta.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 试卷 服务实现类
 * </p>
 *
 * @author zeta
 * @since 2020-08-13
 */
@Service
public class EduPaperServiceImpl extends ServiceImpl<EduPaperMapper, EduPaper> implements EduPaperService {
	@Autowired
	private 	EduQuestionTypeSysService 	QuestionTypeSysService;
	@Autowired
	private EduPaperAndQuestionTypeSysService eduPaperAndQuestionTypeSysService;
	@Override
	public List<PaperListVo> getPaperAndTypeListByCourseId(String courseId) {
		ArrayList<PaperListVo> chapterVoArrayList = new ArrayList<>();
		QueryWrapper<EduPaper> queryWrapper1 = new QueryWrapper<EduPaper>();
		queryWrapper1.eq("course_id", courseId);
		//
		List<EduPaper> paperList = baseMapper.selectList(queryWrapper1);
		
		QueryWrapper<EduPaperAndQuestionTypeSys> queryWrapper2 = new QueryWrapper<EduPaperAndQuestionTypeSys>();
		queryWrapper2.eq("course_id", courseId);
		//
		List<EduPaperAndQuestionTypeSys> list = eduPaperAndQuestionTypeSysService.list(queryWrapper2);		
		List<EduQuestionTypeSys> QuestionTypeSysList = QuestionTypeSysService.list(null);
		HashedMap<String, EduQuestionTypeSys> hashedMap =new  HashedMap<String, EduQuestionTypeSys>();
		for(EduQuestionTypeSys i:QuestionTypeSysList) {
			hashedMap.put(i.getId(), i);
		}
		
		int count1 = paperList.size();
		for (int i = 0; i < count1; i++) {
			EduPaper chapter = paperList.get(i);

			//创建章节vo对象
			PaperListVo chapterVo = new PaperListVo();
			if(chapter!=null) {
				BeanUtils.copyProperties(chapter, chapterVo);
				chapterVoArrayList.add(chapterVo);

			}
			
	
			ArrayList<PaperAndQuestionTypeSysVo> ChapterAndQuestionTypeSysVoList = new ArrayList<PaperAndQuestionTypeSysVo>();
			int count2 = list.size();
			for (int j = 0; j < count2; j++) {

				EduPaperAndQuestionTypeSys video = list.get(j);
				PaperAndQuestionTypeSysVo videoVo = new PaperAndQuestionTypeSysVo();
				if(chapter.getId().equals(video.getPaperId())){

					//创建课时vo对象
				
					if(video!=null) {
						
						BeanUtils.copyProperties(video, videoVo);
						
							videoVo.setQuestionTypeSysName(hashedMap.get(videoVo.getQuestionTypeSysId()).getName());
	
						ChapterAndQuestionTypeSysVoList.add(videoVo);
					}
					
					}
//				ArrayList<paperVoback> paperVolist=new ArrayList<paperVoback>();
//				int count3=papers.size();
//				for(int z=0;z<count3;z++) {
//					EduPaper eduPaper = papers.get(z);
//					
//					if(video.getId().equals(eduPaper.getVideoId())){
//						System.out.println(video.toString());
//						paperVoback paperback=new paperVoback();
//						if(paperback!=null) {
//							BeanUtils.copyProperties(eduPaper, paperback);
//							paperVolist.add(paperback);
//						}
//					}
//				}
//				videoVo.setChildren(paperVolist);
			}
			chapterVo.setChildren(ChapterAndQuestionTypeSysVoList);
		}
		return chapterVoArrayList;
	}
	@Override
	public PaperListVo getPaperByPaperId(String paperId) {
		EduPaper paperList = baseMapper.selectById(paperId);
		QueryWrapper<EduPaperAndQuestionTypeSys> queryWrapper2 = new QueryWrapper<EduPaperAndQuestionTypeSys>();
		queryWrapper2.eq("paper_id", paperId);
		List<EduPaperAndQuestionTypeSys> list = eduPaperAndQuestionTypeSysService.list(queryWrapper2);
		List<EduQuestionTypeSys> QuestionTypeSysList = QuestionTypeSysService.list(null);
		HashedMap<String, EduQuestionTypeSys> hashedMap =new  HashedMap<String, EduQuestionTypeSys>();
		for(EduQuestionTypeSys i:QuestionTypeSysList) {
			hashedMap.put(i.getId(), i);
		}		
		//创建章节vo对象
			PaperListVo chapterVo = new PaperListVo();
			if(paperList!=null) {
				BeanUtils.copyProperties(paperList, chapterVo);
			}
			ArrayList<PaperAndQuestionTypeSysVo> ChapterAndQuestionTypeSysVoList = new ArrayList<PaperAndQuestionTypeSysVo>();
			int count2 = list.size();
			for (int j = 0; j < count2; j++) {

				EduPaperAndQuestionTypeSys video = list.get(j);
				PaperAndQuestionTypeSysVo videoVo = new PaperAndQuestionTypeSysVo();
				if(paperList.getId().equals(video.getPaperId())){

					//创建课时vo对象
				
					if(video!=null) {
						
						BeanUtils.copyProperties(video, videoVo);
						
							videoVo.setQuestionTypeSysName(hashedMap.get(videoVo.getQuestionTypeSysId()).getName());
	
						ChapterAndQuestionTypeSysVoList.add(videoVo);
					}
					
					}
//				ArrayList<paperVoback> paperVolist=new ArrayList<paperVoback>();
//				int count3=papers.size();
//				for(int z=0;z<count3;z++) {
//					EduPaper eduPaper = papers.get(z);
//					
//					if(video.getId().equals(eduPaper.getVideoId())){
//						System.out.println(video.toString());
//						paperVoback paperback=new paperVoback();
//						if(paperback!=null) {
//							BeanUtils.copyProperties(eduPaper, paperback);
//							paperVolist.add(paperback);
//						}
//					}
//				}
//				videoVo.setChildren(paperVolist);
			}
			chapterVo.setChildren(ChapterAndQuestionTypeSysVoList);
		
		return chapterVo;
	}


}
